package com.gyf.controller;


import com.gyf.pojo.User;
import com.gyf.services.UserBizImpl;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Log4j
public class LoginUserController {

    @Autowired
    private UserBizImpl userBiz;

    @RequestMapping("/login")
    public String Login(String userName, String password, HttpSession session, Model model){
        Map<String,Object> map = new HashMap<>();
        log.debug("前端传来的用户名"+userName+"密码"+password);
        map.put("name",userName);
        map.put("password",password);
        User user = userBiz.findUser(map);
        System.out.println(user.toString());
        log.debug("数据库查出来用户"+user.toString());
        session.setAttribute("loginUser",user.getName());
        //重定向到书记页面
        return "redirect:/book/allBook";
    }

    @RequestMapping("/addJsp")
    public String addJsp(){
        return "LogonUser";
    }


    @RequestMapping("/add")
    public String add(User user,Model model){
        int count = userBiz.add(user);
        log.debug("前端传过来的信息"+user.toString());
        if (count>0){
            model.addAttribute("msg","添加成功");
            return "index";
        }else {
            model.addAttribute("msg","添加失败");
            return "LogonUser";
        }

    }
}
